package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.server.dao.StrategyDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.StrategyEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StrategyImpl implements StrategyDao {

    @Override
    public Integer insert(StrategyEntity entity) {
        String sql = "insert into strategy(strategyName,switchOn,modifiedTime,business,controlLevel,type) values (?,?,?,?,?,?)";
        Object[] kk = new Object[6];
        kk[0]=entity.getStrategyName();
        kk[1]=entity.getSwitchOn();
        kk[2] = entity.getModifiedTime();
        kk[3]=entity.getBusiness();
        kk[4]=entity.getControlLevel();
        kk[5]=entity.getType();
        Integer result = JdbcUtil.insert(sql, kk);
        return result;
    }

    @Override
    public boolean update(StrategyEntity entity) {
        String sql = "update strategy set strategyName=?,business=?,controlLevel=?,modifiedTime=? where strategyID=?";
        Object[] kk = new Object[5];
        kk[0]=entity.getStrategyName();
        kk[1]=entity.getBusiness();
        kk[2]=entity.getControlLevel();
        kk[3]=entity.getModifiedTime();
        kk[4]=entity.getStrategyID();
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public boolean updateSwitch(StrategyEntity entity) {
        String sql = "update strategy set modifiedTime=?,switchOn=? where 1= ? and strategyID=?";
        Object[] kk = new Object[4];
        kk[0]=entity.getModifiedTime();
        kk[1]=entity.getSwitchOn();
        kk[2]=1;
        kk[3]=entity.getStrategyID();
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public boolean delete(Object[] ids) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("delete from strategy");
        if (ids != null && ids.length != 0) {
            int length = ids.length;
            buffer.append(" where strategyID in (");
            for (int i = 0; i < length; i++) {
                if (i != length - 1) {
                    buffer.append("?,");
                } else {
                    buffer.append("?)");
                }
            }
        } else {
            return true;
        }
        return JdbcUtil.update(buffer.toString(),ids);
    }

    @Override
    public boolean delete(Integer id) {
        String sql = "delete from strategy,strategy_device,strategy_action where strategyID = ?";
        Integer[] kk = new Integer[1];
        kk[0] = id;
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public int getCountByCondition(String str, String type) {
        StringBuffer buffer = new StringBuffer("select count(1) as result from strategy where 1=1");
        ArrayList<String> list = new ArrayList();
        if(!StringUtil.isEmpty(type)){
            buffer.append(" and type=?");
            list.add(type);
        }
        if(!StringUtil.isEmpty(str)){
            buffer.append(" and strategyName like ?");
            list.add(str+"%");
        }
        String[] kk = list.toArray(new String[0]);
        Map<String, String> map = JdbcUtil.queryForMap(buffer.toString(), kk);
        if(map == null || map.get("result") == null){
            return 0;
        }
        return Integer.parseInt(map.get("result"));
    }

    @Override
    public List<StrategyEntity> findByCondition(String nameLike, String type) {
        StringBuffer buffer = new StringBuffer("select * from strategy where 1=1");
        ArrayList<String> list = new ArrayList();
        if(!StringUtil.isEmpty(type)){
            buffer.append(" and type=?");
            list.add(type);
        }
        if(!StringUtil.isEmpty(nameLike)){
            buffer.append(" and strategyName like ?");
            list.add(nameLike+"%");
        }
        buffer.append(" order by strategyID desc");
        String[] kk = list.toArray(new String[0]);
        List<StrategyEntity> result = JdbcUtil.queryForList(buffer.toString(),kk, StrategyEntity.class);
        return result;
    }

    @Override
    public List<StrategyEntity> findByCondition(String str, String type, int start, int size) {
        StringBuffer buffer = new StringBuffer("select * from strategy where 1=1");
        ArrayList<Object> list = new ArrayList();
        if(!StringUtil.isEmpty(type)){
            buffer.append(" and type=?");
            list.add(type);
        }
        if(!StringUtil.isEmpty(str)){
            buffer.append(" and strategyName like ?");
            list.add(str+"%");
        }
        buffer.append(" order by strategyID desc");
        buffer.append(" limit ?,?");
        list.add(start);
        list.add(size);
        Object[] kk = list.toArray();
        List<StrategyEntity> result = JdbcUtil.queryForList(buffer.toString(),kk, StrategyEntity.class);
        return result;
    }

    @Override
    public StrategyEntity findOne(Integer id) {
        String sql="select * from strategy where strategyID = ?";
        Integer[] kk = new Integer[1];
        kk[0] = id;
        StrategyEntity result = JdbcUtil.queryForBean(sql, kk, StrategyEntity.class);
        return result;
    }

    @Override
    public StrategyEntity findByName(String name) {
        String sql="select * from strategy where 1= ? and strategyName = ?";
        Object[] kk = new Object[2];
        kk[0] = 1;
        kk[1] = name;
        StrategyEntity result = JdbcUtil.queryForBean(sql, kk, StrategyEntity.class);
        return result;
    }

    @Override
    public StrategyEntity findOneByDevice(String sn) {
        String sql="select a.strategyID,a.strategyName,a.switchOn,a.modifiedTime,a.business,a.controlLevel,a.type from strategy as a inner join strategy_device as b on a.strategyID = b.strategyID where b.deviceID = ?";
        Object[] kk = new Object[1];
        kk[0] = sn;
        StrategyEntity result = JdbcUtil.queryForBean(sql, kk, StrategyEntity.class);
        return result;
    }

    @Override
    public List<StrategyEntity> findAllOfOn() {
        String sql = "select * from strategy where switchOn = 1 order by strategyID desc";
        List<StrategyEntity> result = JdbcUtil.queryForList(sql,null, StrategyEntity.class);
        return result;
    }

    @Override
    public List<StrategyEntity> findAll() {
        String sql = "select * from strategy order by strategyID desc";
        List<StrategyEntity> result = JdbcUtil.queryForList(sql,null, StrategyEntity.class);
        return result;
    }

    @Override
    public List<StrategyEntity> findAllAutoOfOn() {
        String sql = "select * from strategy where switchOn = 1 and type=1 order by strategyID desc";
        List<StrategyEntity> result = JdbcUtil.queryForList(sql,null, StrategyEntity.class);
        return result;
    }
}
